﻿using DevExpress.XtraGrid.Demos.util;
using mendian;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.UC.客户CRM.Service
{
    public class 高德地址Service
    {

        public static GuideDTO 行政区查询(String province)
        {
            return
                FunctionHelper.JsonDeserialization<GuideDTO>(
                    RestCall.httpGetMethod(
                        "https://restapi.amap.com/v3/config/district?key=1e35c2196190d3f06060fb90f1c4ce30&keywords=" + province + "&subdistrict=2&extensions=base"
                        )
                    .Replace("[]", "\"\"")
                    );
        }

        public static GaodeAddressDTO getCheckGaodeAddress(String keywords, String city)
        {
            String re = RestCall.httpGetMethod(
                         "https://restapi.amap.com/v3/assistant/inputtips?key=1e35c2196190d3f06060fb90f1c4ce30&keywords=" + keywords
                         + "&type=&location=&city=" + city
                         + "&datatype=all"
                         )
                     .Replace("[]", "\"\"");
            return
                FunctionHelper.JsonDeserialization<GaodeAddressDTO>(
                    re
                    );
        }

        public static GeoDTO checkSaveAddress(String address)
        {
            return
                FunctionHelper.JsonDeserialization<GeoDTO>(
                    RestCall.httpGetMethod(
                        "https://restapi.amap.com/v3/geocode/geo?key=1e35c2196190d3f06060fb90f1c4ce30&address=" + address + "&city="
                        )
                    .Replace("[]", "\"\"")
                    );
        }
        public static GaodeIpDTO getGaodeIp(String ip)
        {
            return
                FunctionHelper.JsonDeserialization<GaodeIpDTO>(
                    RestCall.httpGetMethod(
                        "https://restapi.amap.com/v3/ip?key=1e35c2196190d3f06060fb90f1c4ce30&ip=" + ip
                        )
                    .Replace("[]", "\"\"")
                    );
        }
        public static string GetIP()
        {
            using (var webClient = new WebClient())
            {
                try
                {
                    var temp = webClient.DownloadString("http://ip.42.pl/raw");
                    return temp;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }

    public class GuideDTO
    {
        public String status { get; set; }  //  返回结果状态值
        public String info { get; set; }    //  返回状态说明
        public String infocode { get; set; }    //  状态码
        public SuggestionDTO suggestion { get; set; } = new SuggestionDTO();
        public List<District省> districts { get; set; } = new List<District省>();
        public GuideDTO()
        {

        }
    }

    public class SuggestionDTO
    {
        public List<String> keywords { get; set; }  //  建议关键字列表
        public List<String> cites { get; set; }  //  建议城市列表
        public SuggestionDTO()
        {

        }
    }
    public class District省
    {
        public String citycode { get; set; }    //  
        public String adcode { get; set; }    //  
        public String name { get; set; }    //  
        public String center { get; set; }    //  
        public String level { get; set; }    //  
        public List<DistrictDTO> districts { get; set; } = new List<DistrictDTO>();
        public District省()
        {

        }
    }
    
    public class DistrictDTO
    {
        public String citycode { get; set; }    //  
        public String adcode { get; set; }    //  
        public String name { get; set; }    //  
        public String center { get; set; }    //  
        public String level { get; set; }    //  
        public List<DistrictDTO> districts { get; set; } = new List<DistrictDTO>();
        public DistrictDTO()
        {

        }
    }

    public class GaodeAddressDTO
    {
        public String status { get; set; }
        public String info { get; set; }
        public String infocode { get; set; }
        public String count { get; set; }
        public List<GaodeAddressDetailDTO> tips { get; set; } = new List<GaodeAddressDetailDTO>();
        public GaodeAddressDTO()
        {

        }
    }
    [JsonObject(MemberSerialization.OptOut)]
    public class GaodeAddressDetailDTO
    {
        public String id { get; set; }
        public String name { get; set; }
        public String district { get; set; }
        public String adcode { get; set; }
        public String location { get; set; }
        [JsonIgnore]
        public String address { get; set; }
        public String typecode { get; set; }
        public GaodeAddressDetailDTO()
        {

        }
    }
    
    public class GeoDTO
    {
        public String status { get; set; }
        public String info { get; set; }
        public String infocode { get; set; }
        public String count { get; set; }
        public List<GeocodeDTO> geocodes { get; set; } = new List<GeocodeDTO>();
        public GeoDTO()
        {

        }
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class GeocodeDTO
    {
        public String formatted_address { get; set; }
        public String country { get; set; }
        public String province { get; set; }
        public String citycode { get; set; }
        public String city { get; set; }
        public String district { get; set; }
        [JsonIgnore]
        public List<String> township { get; set; }
        public NeighborhoodDTO neighborhood { get; set; }
        public BuildingDTO building { get; set; }
        public String adcode { get; set; }
        [JsonIgnore]
        public List<String> street { get; set; }
        [JsonIgnore]
        public List<String> number { get; set; }
        public String location { get; set; }
        public String level { get; set; }
        public GeocodeDTO()
        {

        }
    }

    public class NeighborhoodDTO
    {
        public List<String> name { get; set; }
        public List<String> type { get; set; }
        public NeighborhoodDTO()
        {

        }
    }
    public class BuildingDTO
    {
        public List<String> name { get; set; }
        public List<String> type { get; set; }
        public BuildingDTO()
        {

        }
    }
    public class GaodeIpDTO
    {
        public String status { get; set; }
        public String info { get; set; }
        public String infocode { get; set; }
        public String province { get; set; }
        public String city { get; set; }
        public String adcode { get; set; }
        public String rectangle { get; set; }
        public GaodeIpDTO()
        {

        }
    }
}
